jQuery.extend( jQuery.easing,
{
    def: 'easeOutQuad',
    easeInOutExpo: function (x, t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
});
var es = null;
!function ($) {

    $(function(){

    $('[data-ride="animated"]').addClass('invisible');
    $('[data-ride="animated"]').appear();
    $('[data-ride="animated"]').on('appear', function() {
        var $el = $(this), $ani = ($el.data('animation') || 'fadeIn'), $delay;
        if ( !$el.hasClass('animated') ) {
            $delay = $el.data('delay') || 0;
            setTimeout(function(){
                $el.removeClass('invisible').addClass( $ani + " animated" );
            }, $delay);
        }
    });

    $(document).on('click.app','[data-ride="scroll"]',function (e) {
        var $target = this.hash;
        if($($target).length){
            e.preventDefault();
            $('html, body').stop().animate({
                'scrollTop': $($target).offset().top
            }, 1000, 'easeInOutExpo', function () {
                window.location.hash = $target;
            });
        }
    });

    });

}(window.jQuery);
$(document).ready(function(){
    if(window.loginStatus){
        $("#top_menu").append('<a href="/ctrl" class="btn btn-sm btn-success btn-rounded m-l"><strong>进入系统</strong></a>');
    } else {
        $("#top_menu").append('<a id="login_btn" class="btn btn-sm btn-info btn-rounded m-l"><strong>微信扫码登录</strong></a>' +
            '              <div id="qr-block" class="panel panel-default invisible" data-animation="fadeInRight" data-delay="300">' +
            '                <div class="panel-body">' +
            '                  <div class="clearfix text-center">' +
            '                     <div id="qrCodeImg"><div class="loader"></div></div>' +
            '                    <small class="text-muted m-b">使用微信扫描二维码</small>' +
            '                  </div>' +
            '                </div>' +
            '                <footer class="panel-footer bg-info text-center no-padder">' +
            '                  <div class="row no-gutter">' +
            '                    <div class="col-xs-12">' +
            '                      <div class="wrapper">' +
            '                        <small id="qrTips" class="text-muted">正在加载二维码</small>' +
            '                      </div>' +
            '                    </div>' +
            '                  </div>' +
            '                </footer>' +
            '              </div>');
        $("#login_btn").mouseover(function(){
            if(es){
                var $el = $(this).next(), $ani = ($el.data('animation') || 'fadeIn'), $delay;
                if ( !$el.hasClass('animated') ) {
                    $delay = $el.data('delay') || 0;
                    setTimeout(function(){
                        $el.removeClass('invisible').addClass( $ani + " animated" );
                    }, $delay);
                }
            }
        });
        $("#login_btn").click(function(){
            var $el = $(this).next(), $ani = ($el.data('animation') || 'fadeIn'), $delay;
            if ( !$el.hasClass('animated') ) {
                $delay = $el.data('delay') || 0;
                setTimeout(function(){
                    $el.removeClass('invisible').addClass( $ani + " animated" );
                }, $delay);
            }

            $(this).mouseout(function(){
                if ( $el.hasClass('animated') ) {
                    $delay = $el.data('delay') || 0;
                    setTimeout(function(){
                        $el.removeClass($ani + " animated").addClass( "invisible" );
                    }, $delay);
                }
            });
            if(es) return null;
            es = new EventSource('/app/api/login');
            es.onerror = function(e) {
                console.log('error', e);
            };
            es.addEventListener('qrCode',function(e){
                var data = JSON.parse(e.data);
                $("#qrCodeImg").html("<img src=" + data.url + ">")
            });
            es.addEventListener('count',function(e){
                $("#qrTips").html("二维码有效期还剩 " + e.data)
            });
            es.addEventListener('info',function(e){
                $("#qrTips").html(e.data)
            });
            es.addEventListener('user',function(e){
                var info = JSON.parse(e.data);
                console.log("user", info);
                $.get('/app/api/getSession/' + info.newEventKey, function(data,status){
                    switch(status){
                        case "success":
                            if(data.code === "00000"){
                                es.close();
                                $(location).attr('href', '/ctrl');
                            } else {
                                console.log("Data: " + JSON.stringify(data) + "nStatus: " + status);
                            }
                            break;
                        default:
                            console.log("Data: " + JSON.stringify(data) + "nStatus: " + status);
                    }
                })
            });
        });
    }
});